package gui;

import java.io.IOException;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;

/**
 * Class for PDF export
 * 
 * @author Aleksander Zotov
 * 
 */
public class ExportBean {

	/**
	 * Edits the PrimeFaces generated PDF file and adds information about
	 * currently selected simulation
	 * 
	 * @param document
	 *            Primefaces generated document
	 * @throws IOException
	 * @throws BadElementException
	 * @throws DocumentException
	 */
	public void postProcessPDF(Object document) throws IOException,
			BadElementException, DocumentException {

		Document pdf = (Document) document;

		Font titleFont = new Font(Font.TIMES_ROMAN, 18, Font.BOLD);
		Paragraph title = new Paragraph("Berechnung: "
				+ ListModel.getSelectedSimForPDF().getName(), titleFont);

		String resultsStringOld = ListModel.getSelectedSimForPDF().getResults();
		String resultsString = resultsStringOld.replaceAll("<br/>", "\n");
		String refDataStringOld = ListModel.getSelectedSimForPDF().getRefData();
		String refDataString = refDataStringOld.replaceAll("<br/>", "\n");

		PdfPTable table = new PdfPTable(2);
		table.setWidthPercentage(50);

		PdfPCell name = new PdfPCell(new Phrase("Name: "));
		name.setBorder(0);
		PdfPCell nameVal = new PdfPCell(new Phrase(ListModel
				.getSelectedSimForPDF().getName()));
		nameVal.setBorder(0);

		PdfPCell description = new PdfPCell(new Phrase("Beschreibung: "));
		description.setBorder(0);
		PdfPCell descriptionVal = new PdfPCell(new Phrase(ListModel
				.getSelectedSimForPDF().getDescription()));
		descriptionVal.setBorder(0);

		PdfPCell username = new PdfPCell(new Phrase("Benutzername: "));
		username.setBorder(0);
		PdfPCell usernameVal = new PdfPCell(new Phrase(ListModel
				.getSelectedSimForPDF().getUsername()));
		usernameVal.setBorder(0);

		PdfPCell created = new PdfPCell(new Phrase("Erstellt am: "));
		created.setBorder(0);
		PdfPCell createdVal = new PdfPCell(new Phrase(
				(ListModel.getSelectedSimForPDF().getTimeStamp()).toString()));
		createdVal.setBorder(0);

		PdfPCell collisionCount = new PdfPCell(new Phrase(
				"Anzahl Kollisionen: "));
		collisionCount.setBorder(0);
		PdfPCell collisionCountVal = new PdfPCell(new Phrase(
				String.valueOf(ListModel.getSelectedSimForPDF()
						.getCollisionCount())));
		collisionCountVal.setBorder(0);

		PdfPCell collisionDistance = new PdfPCell(new Phrase(
				"Kollisionsabstand: "));
		collisionDistance.setBorder(0);
		PdfPCell collisionDistanceVal = new PdfPCell(new Phrase(
				Float.toString(ListModel.getSelectedSimForPDF()
						.getCollisionDistance())));
		collisionDistanceVal.setBorder(0);

		PdfPCell refSystem = new PdfPCell(new Phrase("Referenzsystem: "));
		refSystem.setBorder(0);
		PdfPCell refSystemVal = new PdfPCell(new Phrase(ListModel
				.getSelectedSimForPDF().getReferenceSystem()));
		refSystemVal.setBorder(0);

		table.addCell(name);
		table.addCell(nameVal);
		table.addCell(description);
		table.addCell(descriptionVal);
		table.addCell(username);
		table.addCell(usernameVal);
		table.addCell(created);
		table.addCell(createdVal);
		table.addCell(collisionCount);
		table.addCell(collisionCountVal);
		table.addCell(collisionDistance);
		table.addCell(collisionDistanceVal);
		table.addCell(refSystem);
		table.addCell(refSystemVal);

		PdfPTable table2 = new PdfPTable(1);
		table2.setWidthPercentage(50);
		PdfPCell simTime = new PdfPCell(new Paragraph("Berechnungszeitraum"));
		simTime.setBorder(Rectangle.BOTTOM | Rectangle.TOP);
		table2.addCell(simTime);

		PdfPTable table3 = new PdfPTable(2);
		table3.setWidthPercentage(50);

		PdfPCell startTime = new PdfPCell(new Paragraph("Startzeit: "));
		startTime.setBorder(0);
		PdfPCell startTimeVal = new PdfPCell(new Paragraph(
				(ListModel.getSelectedSimForPDF().getStartTime()).toString()));
		startTimeVal.setBorder(0);

		PdfPCell endTime = new PdfPCell(new Paragraph("Endzeit: "));
		endTime.setBorder(0);
		PdfPCell endTimeVal = new PdfPCell(new Paragraph(
				(ListModel.getSelectedSimForPDF().getEndTime()).toString()));
		endTimeVal.setBorder(0);

		table3.addCell(startTime);
		table3.addCell(startTimeVal);
		table3.addCell(endTime);
		table3.addCell(endTimeVal);

		PdfPTable table4 = new PdfPTable(1);
		table4.setWidthPercentage(50);
		PdfPCell refData = new PdfPCell(new Paragraph("Referenzdaten"));
		refData.setBorder(Rectangle.BOTTOM | Rectangle.TOP);
		table4.addCell(refData);
		PdfPCell refDataVal = new PdfPCell(new Paragraph(refDataString));
		refDataVal.setBorder(0);
		table4.addCell(refDataVal);

		PdfPTable table5 = new PdfPTable(1);
		table5.setWidthPercentage(50);
		PdfPCell results = new PdfPCell(new Paragraph("Ergebnisse"));
		results.setBorder(Rectangle.BOTTOM | Rectangle.TOP);
		table5.addCell(results);
		PdfPCell resultsVal = new PdfPCell(new Paragraph(resultsString));
		resultsVal.setBorder(0);
		table5.addCell(resultsVal);

		Paragraph emptyLine = new Paragraph();
		emptyLine.add(new Paragraph(" "));
		emptyLine.add(new Paragraph(" "));

		pdf.open();
		pdf.setPageSize(PageSize.A4);
		pdf.addTitle("SatPro Export");
		pdf.add(title);
		pdf.add(emptyLine);
		pdf.add(table);
		pdf.add(emptyLine);
		pdf.add(table2);
		pdf.add(table3);
		pdf.add(emptyLine);
		pdf.add(table4);
		pdf.add(emptyLine);
		pdf.add(table5);
		pdf.close();
	}
}
